define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
                defOperateStatus:[
                    {id: "1", name: "启用", showIds: ["2"]},
                    {id: "2", name: "暂停", showIds: ["1"]}
                ]
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.spread.pageList(param.campaignId,newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",status:"status",statusName:"statusName",unitPrice:"dayBudget",
                            strategyCount:"relevantStrategy",creativeCount:"relevantOriginality",spreadType:"type",objMatterType:"productType",
                            pv:"pv",click:"click",cr:"cr",price:"cpc",cost:"cost"
                        },true);
                        var typeMap=servers.base.getMapSpreadType(newInfo.spreadType.toString()).idMap;
                        newInfo.isOrdinary=typeMap==="1";
                        newInfo.isWeChat=typeMap==="2";
                        newInfo.strategyCount=$.ExtCommon.isCheckEmpty(newInfo.strategyCount)?0:parseInt(newInfo.strategyCount);
                        newInfo.creativeCount=$.ExtCommon.isCheckEmpty(newInfo.creativeCount)?0:parseInt(newInfo.creativeCount);
                        $.ExtCommon.conversionEmptyZero(newInfo,["pv","click","cost","cr","price"]);
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger("bind:PageList");
            });
        },
        getAllList:function (campaignId) {
            var self = this;
            servers.spread.list({campaignId:campaignId},function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{id:"id",name:"name",type:"type",objMatterType:"productType"},true);
                        newInfo.objMatterType=newInfo.objMatterType.toString();
                        var typeMap=servers.base.getMapSpreadType(newInfo.type).idMap;
                        newInfo.isOrdinary=typeMap==="1";
                        newInfo.isWeChat=typeMap==="2";
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data=newJson;
                self.set({dataAllList: res});
                self.trigger('bind:AllList');
            });
        },
        getInfo: function (param) {
            var self = this;
            if (param.id) {
                servers.spread.info(param.campaignId,{id:param.id}, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",name: "name",spreadType: "type",dayBudget: "dayBudget",objMatterType:"productType"
                    }, true);
                    var typeMap=servers.base.getMapSpreadType(newJson.spreadType.toString()).idMap;
                    newJson.isOrdinary=typeMap==="1";
                    newJson.isWeChat=typeMap==="2";
                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo: {isAdd: false, res: res}});
                    self.trigger('bind:Info');
                });
            }else {
                var dataDef={isOrdinary:true,isWeChat:false};
                dataDef.spreadType=servers.base.getMapSpreadType("1",true);
                dataDef.objMatterType=servers.base.getMapSpreadType("1",true);
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=$.ExtCommon.getMappingJson(json, {campaignName: "name",campaignType: "spreadType",dailyBudget:"dayBudget",productType:"objMatterType"});
            servers.spread.add(json.campaignId,data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=$.ExtCommon.getMappingJson(json, {id:"id",campaignName: "name",dailyBudget:"dayBudget"});
            servers.spread.update(json.campaignId,data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateStatus: function (param) {
            var self = this;
            var newParam=$.ExtCommon.getMappingJson(param, {id:"id",configuredStatus:"status"});
            servers.spread.updateStatus(param.campaignId,newParam, function (res) {
                var newJson={};
                if(res.success){
                    newJson={status:param.status};
                    newJson.statusName=param.status==="1"?"有效":"暂停中";
                }
                res.data=newJson;
                self.set({dataUpdStatus: {req:param,res:res}});
                self.trigger("update:Status");
            });
        },
        updatePrice: function (param) {
            var self = this;
            var newParam=$.ExtCommon.getMappingJson(param, {id:"id",dayBudget :"unitPrice"});
            servers.spread.updatePrice(param.campaignId,newParam, function (res) {
                self.set({dataUpdPrice: {req:param,res:res}});
                self.trigger("update:Price");
            });
        }
    });
});